package com.chain.BrotherExchange.ui.adapter;

import android.content.res.Resources;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.model.CoinOrderList;

import java.util.List;

/**
 * 币币订单列表
 */
public class CoinOrderListAdapter extends RecyclerView.Adapter<CoinOrderListAdapter.ViewHolder> {

    private List<CoinOrderList.DataBean.ListBean> mOrderList;
    private AdapterView.OnItemClickListener mOnItemClickListener;

    @NonNull
    @Override
    public CoinOrderListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        return new ViewHolder(LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.item_coin_order, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull CoinOrderListAdapter.ViewHolder holder, int position) {
        CoinOrderList.DataBean.ListBean bean = mOrderList.get(position);
        Resources resources = holder.itemView.getContext().getResources();
        holder.orderDateText.setText(bean.getCreate_time());
        holder.orderStateText.setText(bean.getStatesText());
        holder.coinTypeText.setText(bean.getSymbol());
        holder.orderDirectionText.setText(bean.getDirection());
        holder.entrustedPriceText.setText(bean.getOn_price());
        holder.entrustedSumText.setText(bean.getSum());
        holder.entrustedTotalNumberText.setText(bean.getAll_num());
        holder.tradeText.setText(bean.getTrade_num());
        holder.cancelOrderLayout.setVisibility(bean.isCancel() ? View.VISIBLE : View.GONE);

        holder.entrustedPriceTitleText.setText(String.format(resources.getString(R.string.placeholder_entrusted_price), bean.getPurchaseSymbol()));
        if(bean.getOpt() == 1){ // 买入
            holder.entrustedSumTitleText.setText(String.format(resources.getString(R.string.placeholder_entrusted_sum), bean.getEntrustSymbol()));
            holder.entrustedTotalNumberTitleText.setText(String.format(resources.getString(R.string.placeholder_entrusted_total_number), bean.getPurchaseSymbol()));
            holder.tradeTitleText.setText(String.format(resources.getString(R.string.volume_of_trade), bean.getPurchaseSymbol()));
        } else { // 卖出
            holder.entrustedSumTitleText.setText(String.format(resources.getString(R.string.placeholder_entrusted_sum), bean.getPurchaseSymbol()));
            holder.entrustedTotalNumberTitleText.setText(String.format(resources.getString(R.string.placeholder_entrusted_total_number), bean.getEntrustSymbol()));
            holder.tradeTitleText.setText(String.format(resources.getString(R.string.volume_of_trade), bean.getEntrustSymbol()));
        }

        holder.cancelOrderLayout.setOnClickListener(v ->
                this.mOnItemClickListener.onItemClick(null, v, position, 0));
    }

    public CoinOrderList.DataBean.ListBean getItem(int position){
        return mOrderList.get(position);
    }

    @Override
    public int getItemCount() {
        return mOrderList != null ? mOrderList.size() : 0;
    }

    public void setOrder(List<CoinOrderList.DataBean.ListBean> orderList) {
        this.mOrderList = orderList;
        notifyDataSetChanged();
    }

    public void addOrder(List<CoinOrderList.DataBean.ListBean> orderList) {
        this.mOrderList.addAll(orderList);
        notifyDataSetChanged();
    }

    public void setCancelClickListener(ListView.OnItemClickListener onItemClickListener){
        this.mOnItemClickListener = onItemClickListener;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tradeText;
        private TextView coinTypeText;
        private TextView orderDateText;
        private TextView orderStateText;
        private TextView entrustedSumText;
        private TextView orderDirectionText;
        private TextView entrustedPriceText;
        private LinearLayout cancelOrderLayout;
        private TextView entrustedTotalNumberText;

        private TextView entrustedPriceTitleText;
        private TextView entrustedSumTitleText;
        private TextView entrustedTotalNumberTitleText;
        private TextView tradeTitleText;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            orderDateText = itemView.findViewById(R.id.order_date_text);
            orderStateText = itemView.findViewById(R.id.order_state_text);
            coinTypeText = itemView.findViewById(R.id.coin_type_text);
            orderDirectionText = itemView.findViewById(R.id.order_direction_text);
            entrustedPriceText = itemView.findViewById(R.id.entrusted_price_text);
            entrustedSumText = itemView.findViewById(R.id.entrusted_sum_text);
            entrustedTotalNumberText = itemView.findViewById(R.id.entrusted_total_number_text);
            tradeText = itemView.findViewById(R.id.trade_text);
            cancelOrderLayout = itemView.findViewById(R.id.cancel_order_layout);

            entrustedPriceTitleText = itemView.findViewById(R.id.entrusted_price_title_text);
            entrustedSumTitleText = itemView.findViewById(R.id.entrusted_sum_title_text);
            entrustedTotalNumberTitleText = itemView.findViewById(R.id.entrusted_total_number_title_text);
            tradeTitleText = itemView.findViewById(R.id.trade_title_text);
        }
    }
}
